package com.yzh.homework.homework03;

/**
 * @author 于振晗
 * @title: Comparable
 * @description: TODO 比较接口
 * @date 2020年08月28日
 * @path com.yzh.homework.homework03
 */
public interface Comparable {
    int compareTo(Object obj);
}


class Student implements Comparable{
    private int number;
    private String name;
    private int age;
    private int score;

    public Student(int number, String name, int age, int score) {
        this.number = number;
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public int compareTo(Object obj) {
        Student student = (Student)obj;
        if (this.score > student.score) {
            return 1;
        }else if(this.score == student.score){
            return 0;
        }else{
            return -1;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "number=" + number +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
class News implements Comparable{
    private int id;
    private String title;
    private String content;
    private int number;

    public News(int id, String title, String content, int number) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.number = number;
    }

    @Override
    public int compareTo(Object obj) {
        News news = (News)obj;
        if (this.id > news.id) {
            return 1;
        }else if(this.id == news.id){
            return 0;
        }else{
            return -1;
        }
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", number=" + number +
                '}';
    }
}

class Test{
    public static void main(String[] args) {
        Student student1 = new Student(1,"小李",19,98);
        Student student2 = new Student(2,"小三",19,58);
        int i = student1.compareTo(student2);
        System.out.println(i);

        switch (i){
            case 1:
                System.out.println(student1.toString());
                System.out.println(student2.toString());
                break;
            case 0:
                System.out.println(student1.toString());
                System.out.println(student2.toString());
                break;
            case -1:
                System.out.println(student2.toString());
                System.out.println(student1.toString());
                break;
        }


        News news1 = new News(8,"真相永远只有一个","不可能发生的密室杀人手法",999999);
        News news2 = new News(9,"热搜榜第一竟然做出这样的事...","真相永远都是你不想知道的答案啊",3000);
        int i1 = news1.compareTo(news2);
        switch (i1){
            case 1:
                System.out.println(news1.toString());
                System.out.println(news2.toString());
                break;
            case 0:
                System.out.println(news1.toString());
                System.out.println(news2.toString());
                break;
            case -1:
                System.out.println(news2.toString());
                System.out.println(news1.toString());
                break;
        }
    }
}